<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Base64 编码技术详解</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        pre {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
        }
        .code-keyword {
            color: #569cd6;
        }
        .code-string {
            color: #ce9178;
        }
        .code-comment {
            color: #6a9955;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
            margin: 3rem 0;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fadeInUp">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">Base64 编码技术</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">深入理解网络传输中的编码艺术</p>
            <div class="flex justify-center space-x-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-code mr-2"></i>编码技术
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-network-wired mr-2"></i>网络传输
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-shield-alt mr-2"></i>数据处理
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Why Base64 Section -->
        <section class="mb-16 animate-fadeInUp">
            <div class="bg-white rounded-2xl shadow-lg p-8 md:p-12 card-hover">
                <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                    <i class="fas fa-question-circle text-gradient mr-4"></i>
                    为什么要使用 Base64
                </h2>
                
                <div class="prose prose-lg max-w-none">
                    <p class="text-gray-700 leading-relaxed mb-6">
                        <span class="drop-cap">B</span>ase64 主要用途<strong class="text-purple-600">不是加密</strong>，而是把一些二进制数转成普通字符，方便在网络上传输。由于一些二进制字符在传输协议中属于控制字符，不能直接传送，所以需要转换一下才可以。
                    </p>
                    
                    <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-xl p-6 my-8">
                        <p class="text-gray-700 mb-4">
                            <i class="fas fa-info-circle text-purple-600 mr-2"></i>
                            由于某些系统中只能使用 ASCII 字符，Base64 就是用来将非 ASCII 字符的数据转换成 ASCII 字符的一种方法，Base64 特别适合在 HTTP、MIME 协议下快速传输数据。
                        </p>
                        <div class="flex flex-wrap gap-3 mt-4">
                            <span class="bg-white px-4 py-2 rounded-lg text-sm font-medium text-purple-700">
                                <i class="fas fa-image mr-2"></i>图片传输
                            </span>
                            <span class="bg-white px-4 py-2 rounded-lg text-sm font-medium text-purple-700">
                                <i class="fas fa-file mr-2"></i>文件编码
                            </span>
                            <span class="bg-white px-4 py-2 rounded-lg text-sm font-medium text-purple-700">
                                <i class="fas fa-envelope mr-2"></i>邮件附件
                            </span>
                        </div>
                    </div>
                    
                    <div class="bg-yellow-50 border-l-4 border-yellow-400 p-6 rounded-r-lg my-8">
                        <p class="text-gray-700">
                            <i class="fas fa-exclamation-triangle text-yellow-600 mr-2"></i>
                            <strong>重要提示：</strong>Base64 并非安全领域下的加密解密算法。虽然经常遇到所谓的 base64 的加密解密，但 base64 只能算是一个编码算法，对数据内容进行编码来适合传输。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visual Diagram -->
        <section class="mb-16 animate-fadeInUp">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <h3 class="text-2xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-diagram-project text-gradient mr-3"></i>
                    Base64 编码流程图
                </h3>
                <div class="mermaid">
                    graph LR
                        A[二进制数据] -->|编码| B[Base64字符串]
                        B -->|网络传输| C[目标系统]
                        C -->|解码| D[原始二进制数据]
                        
                        style A fill:#f9f,stroke:#333,stroke-width:2px
                        style B fill:#bbf,stroke:#333,stroke-width:2px
                        style C fill:#bfb,stroke:#333,stroke-width:2px
                        style D fill:#fbf,stroke:#333,stroke-width:2px
                </div>
            </div>
        </section>

        <!-- Code Examples -->
        <section class="mb-16">
            <!-- Base64 Utils -->
            <div class="mb-12 animate-fadeInUp">
                <div class="bg-white rounded-2xl shadow-lg overflow-hidden card-hover">
                    <div class="bg-gradient-to-r from-purple-600 to-pink-600 text-white p-6">
                        <h3 class="text-2xl font-bold flex items-center">
                            <i class="fas fa-tools mr-3"></i>
                            Base64 工具类
                        </h3>
                    </div>
                    <div class="p-8">
                        <pre><code><span class="code-keyword">import</span> java.util.Base64;

<span class="code-keyword">public class</span> Base64Utils {

    <span class="code-comment">// 解码器</span>
    <span class="code-keyword">private static</span> Base64.Decoder decoder = Base64.getDecoder();

    <span class="code-comment">// 编码器</span>
    <span class="code-keyword">private static</span> Base64.Encoder encoder = Base64.getEncoder();

    <span class="code-comment">// 使用Base64把字节数组编码成字符串</span>
    <span class="code-keyword">public static</span> String encode(byte[] bytes) {
        <span class="code-keyword">return</span> encoder.encodeToString(bytes);
    }

    <span class="code-comment">// 使用Base64字符串解码成字节数组</span>
    <span class="code-keyword">public static</span> byte[] decode(String data) {
        <span class="code-keyword">return</span> decoder.decode(data);
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <!-- String Encoding Example -->
            <div class="mb-12 animate-fadeInUp">
                <div class="bg-white rounded-2xl shadow-lg overflow-hidden card-hover">
                    <div class="bg-gradient-to-r from-blue-600 to-cyan-600 text-white p-6">
                        <h3 class="text-2xl font-bold flex items-center">
                            <i class="fas fa-font mr-3"></i>
                            Base64 解编码字符串
                        </h3>
                    </div>
                    <div class="p-8">
                        <pre><code>@Test
<span class="code-keyword">void</span> contextLoads() <span class="code-keyword">throws</span> Exception {
    String msg = <span class="code-string">"Hello 你好"</span>;
    String encode = Base64Utils.encode(msg.getBytes(<span class="code-string">"utf-8"</span>));
    System.out.println(<span class="code-string">"base64编码后的内容："</span> + encode);
    
    byte[] decode = Base64Utils.decode(encode);
    System.out.println(<span class="code-string">"base64解码后的内容:"</span> + 
        <span class="code-keyword">new</span> String(decode, 0, decode.length, <span class="code-string">"utf-8"</span>));
}</code></pre>
                        
                        <div class="mt-6 bg-gray-100 rounded-lg p-4">
                            <p class="text-sm text-gray-600 mb-2">输出结果：</p>
                            <div class="bg-white rounded p-3 font-mono text-sm">
                                <p class="text-green-600">base64编码后的内容：SGVsbG8g5L2g5aW9</p>
                                <p class="text-blue-600">base64解码后的内容：Hello 你好</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Binary Data Example -->
            <div class="animate-fadeInUp">
                <div class="bg-white rounded-2xl shadow-lg overflow-hidden card-hover">
                    <div class="bg-gradient-to-r from-green-600 to-teal-600 text-white p-6">
                        <h3 class="text-2xl font-bold flex items-center">
                            <i class="fas fa-file-image mr-3"></i>
                            Base64 解编码二进制数据
                        </h3>
                    </div>
                    <div class="p-8">
                        <pre><code>@Test
<span class="code-keyword">void</span> contextLoads2() <span class="code-keyword">throws</span> Exception {
    <span class="code-comment">// 二进制文件</span>
    File file = <span class="code-keyword">new</span> File(<span class="code-string">"D:\\code\\idea2020\\test\\frame\\jwt\\src\\main\\resources\\Redis.png"</span>);
    byte[] bytes = FileCopyUtils.copyToByteArray(file);

    <span class="code-comment">// 把二进制文件用Base64编码成字符串，方便在网络传输</span>
    String encode = Base64Utils.encode(bytes);
    System.out.println(<span class="code-string">"二进制文件base64编码后的内容:"</span> + encode);

    <span class="code-comment">// 把Base64编码的二进制字符串解码成字节数组</span>
    byte[] decode = Base64Utils.decode(encode);
    <span class="code-keyword">new</span> FileOutputStream(<span class="code-string">"./demo.png"</span>).write(decode);
}</code></pre>
                        
                        <div class="mt-6 bg-gradient-to-r from-green-50 to-teal-50 rounded-lg p-6">
                            <h4 class="font-semibold text-gray-800 mb-3">
                                <i class="fas fa-lightbulb text-green-600 mr-2"></i>
                                应用场景
                            </h4>
                            <div class="grid md:grid-cols-2 gap-4">
                                <div class="bg-white rounded-lg p-4">
                                    <i class="fas fa-image text-green-600 text-2xl mb-2"></i>
                                    <p class="text-sm text-gray-700">图片嵌入 HTML/CSS</p>
                                </div>
                                <div class="bg-white rounded-lg p-4">
                                    <i class="fas fa-envelope text-blue